<?php
/**
 * Created by PhpStorm.
 * User: JSQ-SL
 * Date: 2018/11/19
 * Time: 15:37
 */

namespace app\index\table;

use app\index\controller\Login;
use think\Db;
use app\index\service\ExcelService;

class ArchiveFurnTable extends BaseTable
{
    protected static $table = 'ARCHIVE_FURN';

    static public function table(){
        $sql = "(
        select 
        f.ARCHIVE_YEARMONTH
        ,f.FURN_NO
        ,f.FURN_NAME
        ,to_char(f.ENTER_TIME,'yyyy-mm-dd') ENTER_TIME
        ,f.FURNTYPE_NO
        ,f.DEPART_ID
        ,d1.DEPART
        ,d2.COLLEGE_ID
        ,d2.COLLEGE
        ,f.SPEC
        ,f.MODEL
        ,f.PRICE
        ,f.FACTORY
        ,f.COUNTRY_ID
        ,d3.COUNTRY_CNNAME COUNTRY
        ,f.FACTORY_NO
        ,to_char(f.FACTORY_DATE,'yyyy-mm-dd') FACTORY_DATE
        ,to_char(f.GET_DATE,'yyyy-mm-dd') GET_DATE
        ,f.STATE_ID
        ,d4.STATE
        ,f.GET_PERSON
        ,f.CHECK_PERSON
        ,f.CHARGE_PERSON
        ,f.REMARK
        ,f.INPUT_PERSON
        ,d5.NAME INPUT_NAME
        ,to_char(f.INPUT_TIME,'yyyy-mm-dd') INPUT_TIME
        ,f.GB
        ,d6.NAME GB_NAME
        ,f.ZJLY
        ,d7.ZIJIN
        ,f.ZJLY_DETAIL
        ,f.BELONG_ID
        ,d8.BELONG
        ,f.ASSET_TYPE_ID
        ,d9.CNAME94 ASSET_TYPE
        ,f.BRAND
        from app_asset.archive_furn f
        left join app_asset.DICT_SB_DEPARTMENT d1 on d1.depart_id=f.depart_id
        left join app_asset.DICT_COLLEGE d2 on d2.COLLEGE_ID=d1.DEPART_BELONG
        left join app_asset.DICT_COUNTRY d3 on d3.COUNTRY_ID=f.COUNTRY_ID
        left join app_asset.FURN_DICT_STATE d4 on d4.STATE_ID=f.STATE_ID
        left join app_asset.EMPLOYEE d5 on d5.STAFF_ID=f.INPUT_PERSON
        left join app_asset.FURN_DICT_GB d6 on d6.GBCODE=f.GB
        left join app_asset.DICT_FURN_ZIJIN d7 on d7.ID=f.ZJLY
        left join app_asset.ASSET_DICT_BELONG d8 on d8.BELONG_ID=f.BELONG_ID
        left join app_asset.ASSET_DICT_TYPE94TO2010 d9 on d9.ccode94=f.asset_type_id
        )";
        return Db::table($sql);
    }

    static public function insert($data){
        $yearmonth = $data['ARCHIVE_YEARMONTH'];
        $furnNo = $data['FURN_NO'];
        self::toDate($data,'ENTER_TIME');
        self::toDate($data,'FACTORY_DATE');
        self::toDate($data,'GET_DATE');
        self::toDate($data,'INPUT_TIME');

        $old = Db::table(static::$table)->where(['ARCHIVE_YEARMONTH'=>$yearmonth,'FURN_NO'=>$furnNo])->find();
        if(empty($old)){
            return Db::table(static::$table)->insert($data);
        }
        else{
            return Db::table(static::$table)
                ->where(['ARCHIVE_YEARMONTH'=>$yearmonth,'FURN_NO'=>$furnNo])
                ->update($data);
        }
    }

}